package com.studyjava.guessdx;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Xml;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import org.xmlpull.v1.XmlPullParser;

import java.io.InputStream;



public class MainActivity extends AppCompatActivity {

//    private Button button;
    private int userScore;
    private ImageView imageView;
    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        //顶部图片
        imageView = findViewById(R.id.imageView);
        //获取按钮id
        Button go_guessname = findViewById(R.id.guessnamebutton);
        // 选曲绘按钮的三个部分
        View buttonLeft = findViewById(R.id.buttonLeft);
        View buttonCenter = findViewById(R.id.buttonCenter);
        View buttonRight = findViewById(R.id.buttonRight);
        Button helpbutton = findViewById(R.id.helpbutton);
        Button cpsbutton = findViewById(R.id.cpsbutton);
        Button bpmbutton = findViewById(R.id.bpmbutton);
        Button userCenterButton = findViewById(R.id.userCenterButton);

        //用户中心按钮
        userCenterButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, usercenter.class);
                startActivity(intent);
            }
        });



        //猜曲名按钮
        go_guessname.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, guessname.class);
                startActivity(intent);
            }
        });

        // 左侧：经典模式
        buttonLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 在这里启动经典模式的Activity
                 Intent intent = new Intent(MainActivity.this, choose_pic_blood.class);
                 startActivity(intent);
            }
        });

        // 中间：禅意模式
        buttonCenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 在这里启动禅意模式的Activity
                 Intent intent = new Intent(MainActivity.this, choosepic.class);
                 startActivity(intent);
            }
        });

        // 右侧：挑战模式
        buttonRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 在这里启动挑战模式的Activity
                 Intent intent = new Intent(MainActivity.this, choose_pic_time.class);
                 startActivity(intent);
            }
        });

        //永琳帮帮我按钮
        helpbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 在这里启动挑战模式的Activity
                Intent intent = new Intent(MainActivity.this, help.class);
                startActivity(intent);
            }
        });

        // 设置ImageView点击事件
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playAudio();
            }
        });

        cpsbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 在这里启动cps的Activity
                Intent intent = new Intent(MainActivity.this, cps.class);
                startActivity(intent);
            }
        });

        bpmbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 在这里启动bpm的Activity
                Intent intent = new Intent(MainActivity.this, bpm.class);
                startActivity(intent);
            }
        });


    loadUserScore();
    checkScore();
    }

    //顶部图片
    private void loadUserScore() {
        try {
            InputStream inputStream = openFileInput("userdata.xml");
            XmlPullParser parser = Xml.newPullParser();
            parser.setInput(inputStream, "UTF-8");

            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String tagName = parser.getName();
                if (eventType == XmlPullParser.START_TAG && "Score".equals(tagName)) {
                    userScore = Integer.parseInt(parser.nextText());
                }
                eventType = parser.next();
            }
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void checkScore() {
        // 如果用户分数达到1000，则更换图片
        if (userScore >= 1000) {
            imageView.setImageResource(R.drawable._1_02);
        }
    }
    private void playAudio() {
        // 如果已经在播放，先释放资源
        if (mediaPlayer != null) {
            mediaPlayer.release();
            mediaPlayer = null;
        }

        // 初始化MediaPlayer并播放音频
        mediaPlayer = MediaPlayer.create(this, R.raw.donttouch);
        mediaPlayer.start();

        // 设置播放完成的监听器，播放完成后释放资源
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.release();  // 释放MediaPlayer资源
                mediaPlayer = null;
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 确保退出Activity时释放MediaPlayer资源
        if (mediaPlayer != null) {
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }
}
